package utils
{
	

	public class DateUtil
	{
		
		/**
		 * 计算年龄
		 */		
		public static function getAgeByBirthday(brithday:String):String
		{
			var c:String=brithday;
			var y0:Number=Number(c.substr(0, 4));
			var m0s:String=c.substr(5, 2);
			var m0:Number=m0s.charAt(0) == '0' ? Number(m0s.substr(1, 1)) : Number(m0s);
			var d0s:String=c.substr(8, 2);
			var d0:Number=d0s.charAt(0) == '0' ? Number(d0s.substr(1, 1)) : Number(d0s);
			var bdate:Date = new Date(y0, m0-1, d0);
			var years:uint = int( (new Date().time - bdate.time)/1000/60/60/24/365 );
			return years+"岁";
		}
		
		/**
		 * 返回一条以时间数字排列的字符串，如201401051159
		 * @param date
		 * @return 
		 */		
		public static function getFileName(date:Date):String
		{
			var str:String = dateToString(date).replace(/-/g, "");
			var hours:String = date.getHours().toString();
			var minutes:String = date.getMinutes().toString();
			var seconds:String = date.getSeconds().toString();
			if(hours.length == 1)
				hours = "0" + hours;
			if(minutes.length == 1)
				minutes = "0" + minutes;
			if(seconds.length == 1)
				seconds = "0" + seconds;
			return str + hours + minutes + seconds;
		}
		
		/**
		 * 计算距今时间长度并返回相应字符串，如“刚刚”、“x分钟前”...
		 * @param date
		 * @return 
		 * 
		 */		
		public static function getTimeLength(created_at:String):String
		{
			var c:String=created_at;
			var date:Date=new Date();
			
			var y0:Number=Number(c.substr(0, 4));
			var y1:Number=date.getUTCFullYear();
			if (y1 > y0)
			{
				return (y1 - y0).toString() + '年前';
			}
			
			var m0s:String=c.substr(5, 2);
			var m0:Number=m0s.charAt(0) == '0' ? Number(m0s.substr(1, 1)) : Number(m0s);
			var m1:Number=date.getUTCMonth() + 1;
			if (m1 > m0)
			{
				return (m1 - m0).toString() + '月前';
			}
			
			var d0s:String=c.substr(8, 2);
			var d0:Number=d0s.charAt(0) == '0' ? Number(d0s.substr(1, 1)) : Number(d0s);
			var d1:Number=date.getUTCDate();
			if (d1 > d0)
			{
				return (d1 - d0).toString() + '天前';
			}
			
			var h0s:String=c.substr(11, 2);
			var h0:Number=h0s.charAt(0) == '0' ? Number(h0s.substr(1, 1)) : Number(h0s);
			var h1:Number=date.getUTCHours();
			if (h1 > h0)
			{
				return (h1 - h0).toString() + '小时前';
			}
			
			var mi0s:String=c.substr(14, 2);
			var mi0:Number=mi0s.charAt(0) == '0' ? Number(mi0s.substr(1, 1)) : Number(mi0s);
			var mi1:Number=date.getUTCMinutes();
			if (mi1 > mi0)
			{
				return (mi1 - mi0).toString() + '分钟前';
			}
			
			return '刚刚';
		}
		
		/**
		 * 根据date对象生成相应格式字符串
		 * @param date
		 * @param style "mm"表示月份，"dd"表示日期，"yy"表示年份，将三个元素以"-"相连，例："mm-dd-yy"
		 * @return
		 */		
		public static function dateToString(date:Date, style:String="yy-mm-dd"):String
		{
			var arr:Array = style.split("-");
			var obj:Object = {};
			obj.yy = date.getFullYear();
			obj.mm = date.getMonth() + 1;
			if(obj.mm < 10)
				obj.mm = "0" + obj.mm;
			obj.dd = date.getDate();
			if(obj.dd < 10)
				obj.dd = "0" + obj.dd;
			return obj[arr[0]] + "-" + obj[arr[1]] + "-" + obj[arr[2]];
		}
		
		/**
		 * 根据date对象生成相应格式字符串
		 * @param string 字符串信息
		 * @param style 字符串格式信息： "mm"表示月份，"dd"表示日期，"yy"表示年份，将三个元素以"-"相连，例："mm-dd-yy"
		 * @return
		 */
		public static function stringToDate(string:String, style:String="yy-mm-dd"):Date
		{
			var arr:Array = string.split("-");
			var styleArr:Array = style.split("-");
			var obj:Object = {};
			obj.yy = int( arr[styleArr.indexOf("yy")] );
			obj.mm = int( arr[styleArr.indexOf("mm")] ) - 1;
			obj.dd = int( arr[styleArr.indexOf("dd")] );
			return new Date(obj.yy, obj.mm, obj.dd);
		}
		
		/**
		 * 返回星座
		 */		
		public static function getConstellationByDate(birthday:String):String
		{
			var m0s:String=birthday.substr(5, 2);
			var m0:Number=m0s.charAt(0) == '0' ? Number(m0s.substr(1, 1)) : Number(m0s);
			var d0s:String=birthday.substr(8, 2);
			var d0:Number=d0s.charAt(0) == '0' ? Number(d0s.substr(1, 1)) : Number(d0s);
			var date:Date = new Date(2000, m0-1, d0);
			
			var date_0:Date = new Date(2000, 0, 20);		//水瓶座起始时间
			var date_1:Date = new Date(2000, 1, 19);		//双鱼
			var date_2:Date = new Date(2000, 2, 21);		//白羊
			var date_3:Date = new Date(2000, 3, 20);		//金牛
			var date_4:Date = new Date(2000, 4, 21);		//双子
			var date_5:Date = new Date(2000, 5, 22);		//巨蟹
			var date_6:Date = new Date(2000, 6, 23);		//狮子
			var date_7:Date = new Date(2000, 7, 23);		//处女
			var date_8:Date = new Date(2000, 8, 23);		//天秤
			var date_9:Date = new Date(2000, 9, 24);		//天蝎
			var date_10:Date = new Date(2000, 10, 23);		//射手
			var date_11:Date = new Date(2000, 11, 22);		//摩羯
			
			if(date.time - date_0.time < 0 || date.time - date_11.time >= 0)
				return "摩羯座";
			else if(date.time - date_10.time >= 0)
				return "射手座";
			else if(date.time - date_9.time >= 0)
				return "天蝎座";
			else if(date.time - date_8.time >= 0)
				return "天秤座";
			else if(date.time - date_7.time >= 0)
				return "处女座";
			else if(date.time - date_6.time >= 0)
				return "狮子座";
			else if(date.time - date_5.time >= 0)
				return "巨蟹座";
			else if(date.time - date_4.time >= 0)
				return "双子座";
			else if(date.time - date_3.time >= 0)
				return "金牛座";
			else if(date.time - date_2.time >= 0)
				return "白羊座";
			else if(date.time - date_1.time >= 0)
				return "双鱼座";
			else if(date.time - date_0.time >= 0)
				return "水瓶座";
			else
				return "";
		}
	}
}